package main

import (
	"fmt"
	"strconv"
)

func main() {
	fmt.Println(split_into_list(2, "011235"))
	fmt.Println(split_into_list(5, "34561245892364388201628"))
}

/**
 * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
 * 输入N和字符S
 * @param N int整型 第N+1个元素是前面N个元素之和， N>=2, N<=5
 * @param S string字符串 需要解析的字符串，字符串长度不超过1000
 * @return int整型一维数组
 */
func split_into_list(N int, S string) []int {
	// write code here
	ls := make([]int, N)

	return f(ls, 0, 0, N, S)
}

func f(ls []int, ci, cs, N int, S string) []int {
	if ci == len(ls) {
		return getAns(ls, S, N)
	}

	for i := 1; i < 1000; i++ {
		cs += i
		if i > len(S)-cs {
			return nil
		}
		ls[ci] = i
		res := f(ls, ci+1, cs, N, S)
		if res != nil {
			return res
		}
		cs -= i
	}

	return nil
}

func getAns(ls []int, S string, N int) []int {
	res := make([]int, len(ls))
	i, j, sum := 0, 0, 0
	for _, l := range ls {
		res[i], _ = strconv.Atoi(S[j : j+l])
		sum += res[i]
		i++
		j += l
	}

	var s string
	for j < len(S) {
		s = strconv.Itoa(sum)

		if j+len(s) <= len(S) && S[j:j+len(s)] == s {
			i, _ = strconv.Atoi(S[j : j+len(s)])
			res = append(res, i)
			sum += i - res[len(res)-N-1]
			j += len(s)
		} else {
			return nil
		}
	}

	return res
}
